%% Author: Administrator
%% Created: 2009-3-21
%% Description: TODO: Add description to app_util
-module(app_util).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([getRandomId/0, ensure_base_apps/0, init_logger/1, init_default_logger/1, get_host_addr_str/0, get_host_addr_str/1, get_local_ip/0]).

%%
%% API Functions
%%
-spec(getRandomId/0 :: () -> pos_integer()).
getRandomId() ->
    crypto:rand_uniform(1, 65536 * 65536).

ensure_base_apps() ->
    application:start(sasl),
    application:start(log4erl),
    crypto:start(),
    ok.

-spec init_logger(Logger :: atom()) -> any().
init_logger(Logger) ->
    log4erl:add_logger(Logger),
    log4erl:add_file_appender(Logger, file3, {"./logs", atom_to_list(Logger) ++ "_logger", {size, 100000}, 5, "elog", debug}),
    log4erl:change_format(Logger, file3, "%j %T [%L] %l%n").

-spec init_default_logger(AppName :: string()) -> any().
init_default_logger(AppName) ->
    File = list_to_atom(AppName),
    log4erl:add_file_appender(File, {"./logs", AppName ++ "_logger", {size, 100000}, 5, "elog", debug}),
    log4erl:change_format(File, "%j %T [%L] %l%n").

-spec get_host_addr_str() -> string().
get_host_addr_str() ->
    ip_string(get_local_ip()).

get_host_addr_str(Ip) ->
    ip_string(Ip).
    
ip_string({I1,I2,I3,I4}) ->
    integer_to_list(I1) ++ "." ++ integer_to_list(I2) ++ "." ++ integer_to_list(I3) ++ "." ++ integer_to_list(I4).

get_local_ip() ->
    {ok, HostName} = inet:gethostname(),
    {ok, Ip} = inet:getaddr(HostName, inet),
    Ip.



%%
%% Local Functions
%%

